import com.sun.xml.internal.bind.v2.model.core.ID;

import javax.naming.spi.DirStateFactory;
import java.sql.*;
import java.util.Scanner;

public class main {
    public static void main(String[] args) {
        //查询
        //m1();
        //条件查询
        //m2();
        //增加
        //m3();
        //删除
        m4();
        //修改
      // m5();





    }



    private static void m5() {
        //1.加载驱动
        //1.加载驱动
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入选择的id");
            int id = sc.nextInt();
            System.out.println("请输入要修改的姓名");
            String name = sc.next();
            System.out.println("请输入要修改的性别");
            String sex = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接(url,password,username)
            String url="jdbc:mysql://127.0.0.1:3306/stu_user?characterEncoding=UTF-8";
            try {

                Connection connection = DriverManager.getConnection(url, "root", "ran09105019");
               /* if (connection!=null){
                    System.out.println("连接成功");
                }*/

                String sql="UPDATE user SET user_name = ?,user_sex = ? WHERE user_id = ? ";


                //4.预编译sql
                PreparedStatement prepareStatement = connection.prepareStatement(sql);

                //5.有参数设置参数
                prepareStatement.setObject(3,id);
                prepareStatement.setObject(2,sex);
                prepareStatement.setObject(1,name);
                /*prepareStatement.setObject(3,sex);*/
                //6.执行sql
                int result = prepareStatement.executeUpdate();
                //7.结果判断
                if (result > 0) {
                    System.out.println("修改成功");
                }
                //8.关资源

                prepareStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }
    private static void m4() {
        // 1、加载驱动
        try {

            Class.forName("com.mysql.cj.jdbc.Driver");
            Scanner scanner =new Scanner(System.in);
            System.out.println("请输入要删除的序号");
            int id= scanner.nextInt();
            // 2、获取连接 (url username password)
            String url = "jdbc:mysql://127.0.0.1:3306/stu_user?characterEncoding=UTF-8";
            Connection connection = DriverManager.getConnection(url, "root", "ran09105019");
//            if (connection != null) {
//                System.out.println("连接成功");
//            }
            // 3、写sql
            String sql = "delete from user where user_id = ? ";

            // 4、预编译sql
            PreparedStatement preparedStatement = connection.prepareStatement(sql);

            // 5、有参数设置参数
            preparedStatement.setObject(1,id);

            // 6、执行sql  executeUpdate
            int result = preparedStatement.executeUpdate();

            // 7、结果判断
            if (result > 0) {
                System.out.println("删除成功");
            }

            // 8、关闭资源
            preparedStatement.close();
            connection.close();


        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
    }
    private static void m3() {
        //1.加载驱动
        //1.加载驱动
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要增加的id");
            String id = sc.next();
            System.out.println("请输入要增加的姓名");
            String name = sc.next();
            System.out.println("请输入要增加的性别");
            String sex = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接(url,password,username)
            String url="jdbc:mysql://127.0.0.1:3306/stu_user?characterEncoding=UTF-8";
            try {
                Connection connection = DriverManager.getConnection(url, "root", "ran09105019");
               /* if (connection!=null){
                    System.out.println("连接成功");
                }*/
                String sql="insert into user(user_id,user_name,user_sex) values (?,?,?)";

                //4.预编译sql
                PreparedStatement prepareStatement = connection.prepareStatement(sql);

                //5.有参数设置参数
                prepareStatement.setObject(1,id);
                prepareStatement.setObject(2,name);
                prepareStatement.setObject(3,sex);
                //6.执行sql
                int result = prepareStatement.executeUpdate();
                //7.结果判断
                if (result > 0) {
                    System.out.println("新增成功");
                }
                //8.关资源

                prepareStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }



    private static void m2() {
        //1.加载驱动
        try {
            Scanner sc = new Scanner(System.in);
            System.out.println("请输入要查询的姓名");
            String word = sc.next();

            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接(url,password,username)
            String url="jdbc:mysql://127.0.0.1:3306/stu_user?characterEncoding=UTF-8";
            try {
                Connection connection = DriverManager.getConnection(url, "root", "ran09105019");
               /* if (connection!=null){
                    System.out.println("连接成功");
                }*/
                String sql="select * from user where user_name like ?";

                //4.预编译sql
                PreparedStatement prepareStatement = connection.prepareStatement(sql);

                //5.有参数设置参数

                //6.执行sql
                 prepareStatement.setObject(1,"%"+word+"%");
                ResultSet resultSet =prepareStatement.executeQuery();
                //7.遍历结果集
                while (resultSet.next()){
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String sex = resultSet.getString(3);
                    System.out.println(id+"----"+name+"----"+sex);

                }
                //8.关资源
                resultSet.close();
                prepareStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }

    private static void m1() {
        //1.加载驱动
        try {
            Class.forName("com.mysql.cj.jdbc.Driver");

            //2.获取连接(url,password,username)
            String url="jdbc:mysql://127.0.0.1:3306/stu_user?charactern=UTF-8";
            try {
                Connection connection = DriverManager.getConnection(url, "root", "ran09105019");
               /* if (connection!=null){
                    System.out.println("连接成功");
                }*/
                String sql="select * from user";

                //4.预编译sql
                PreparedStatement prepareStatement = connection.prepareStatement(sql);

                //5.有参数设置参数

                //6.执行sql
                ResultSet resultSet = prepareStatement.executeQuery();
                //7.遍历结果集
                while (resultSet.next()){
                    int id = resultSet.getInt(1);
                    String name = resultSet.getString(2);
                    String sex = resultSet.getString(3);
                    System.out.println(id+"----"+name+"----"+sex);

                }
                //8.关资源
                resultSet.close();
                prepareStatement.close();
                connection.close();
            } catch (SQLException e) {
                e.printStackTrace();
            }


        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }

    }
}
